package com.magus.project.yddemo.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.magus.framework.core.controller.BaseController;
import com.magus.framework.core.persistence.SearchFilter;
import com.magus.framework.system.annotation.AppFunctionInfo;
import com.magus.framework.system.annotation.AppFunctionInfo.FunctionType;
import com.magus.framework.system.annotation.AppGroupInfo;
import com.magus.framework.system.entity.Company;
import com.magus.framework.system.service.CompanyService;
import com.magus.framework.system.util.SecurityUtils;
import com.magus.project.yddemo.entity.IndicatorData;
import com.magus.project.yddemo.entity.IndicatorDataPlant;
import com.magus.project.yddemo.entity.IndicatorDetail;
import com.magus.project.yddemo.service.IndicatorDataPlantService;
import com.magus.project.yddemo.service.IndicatorDetailService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 年指标填报
 * 
 * @author xujuan
 * @date 2018-01-19 15:26:53
 */
@Api(tags = "IndicatorDataPlantYearManager", description = "年指标填报")
@RestController
@RequestMapping("/yddemo/plantYears")
@AppGroupInfo(key = "yddemo:plantYear", name = "年指标填报", appKey = "yddemo")
public class IndicatorDataPlantYearController extends BaseController {

	@Autowired
	private IndicatorDataPlantService dataPlantService;

	@Autowired
	private IndicatorDetailService indicatorDetailService;

	@Autowired
	private CompanyService companyService;

	SimpleDateFormat day = new SimpleDateFormat("yyyy");

	/**
	 * 查询年指标
	 * 
	 * @param date
	 *            日期
	 * @param companyId
	 *            电厂
	 * @return 指标数据集合
	 */
	@GetMapping
	@RequiresPermissions("yddemo:plantYear:view")
	@ApiOperation(value = "查询年指标", notes = "查询年指标")
	@AppFunctionInfo(key = "yddemo:plantYear:view", name = "查询年指标列表", groupKey = "yddemo:plantYear", //
			isMenu = false, type = FunctionType.PAGE, url = "/yddemo/plantYears/page/view")
	ResponseEntity<List<IndicatorDataPlant>> findAll(
			// @RequestParam(required = false) String companyId,
			@RequestParam(required = false) String date) {
		Date bDate = new Date();
		try {
			bDate = day.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		// 找到当前登陆用户对应的公司
		String companyId = "";
		List<String> orgCodeList = SecurityUtils.findCurrentOrgScope();
		if (orgCodeList != null && orgCodeList.size() > 0) {
			String uniqueKey = orgCodeList.get(0);
			Company company = companyService.findByUniqueKey(uniqueKey);
			companyId = company.getId();
		}

		List<SearchFilter> searchFilters = new ArrayList<>();
		// if (StringUtils.isNotBlank(companyId)) {
		searchFilters.add(SearchFilter.equal("indicatorDetail.company.id", companyId));
		// }
		searchFilters.add(SearchFilter.equal("time", bDate.getTime()));
		searchFilters.add(SearchFilter.equal("dimension", "year"));
		searchFilters.add(SearchFilter.like("indicatorDetail.indicator.dimension", "year"));
		searchFilters.add(SearchFilter.like("indicatorDetail.dimension", "year"));

		Specification<IndicatorDataPlant> spec = buildFilterRequest(searchFilters);
		List<IndicatorDataPlant> result = dataPlantService.findAll(spec);

		if (result.isEmpty()) {
			List<SearchFilter> recordSearchFilters = new ArrayList<>();
			recordSearchFilters.add(SearchFilter.like("indicator.dimension", "year"));
			recordSearchFilters.add(SearchFilter.like("dimension", "year"));
			// if (StringUtils.isNotBlank(companyId)) {
			recordSearchFilters.add(SearchFilter.equal("company.id", companyId));
			// }

			Specification<IndicatorDetail> recordSpec = buildFilterRequest(recordSearchFilters);
			List<IndicatorDetail> detailList = indicatorDetailService.findAll(recordSpec);

			result = dataPlantService.getIndicatorDataPlantYearList(detailList);
		}

		return ResponseEntity.ok(result);
	}

	/**
	 * 保存年指标
	 * 
	 */
	@PutMapping(params = { "save", "date" })
	@RequiresPermissions("yddemo:plantYear:save")
	@ApiOperation(value = "保存年指标", notes = "返回成功保存后的对象")
	@AppFunctionInfo(key = "yddemo:plantYear:save", name = "保存年指标", groupKey = "yddemo:plantYear", //
			isMenu = false, type = FunctionType.BUTTON)
	ResponseEntity<List<IndicatorDataPlant>> save(@RequestBody List<IndicatorDataPlant> dataPlants,
			@RequestParam(required = true) String date) {
		dataPlants = dataPlantService.saveIndicatorDataPlantYears(dataPlants, date);
		return ResponseEntity.ok(dataPlants);
	}

	/**
	 * 上传年指标
	 * 
	 */
	@PutMapping(params = { "report", "date" })
	@RequiresPermissions("yddemo:plantYear:report")
	@ApiOperation(value = "上传年指标", notes = "返回成功保存后的对象")
	@AppFunctionInfo(key = "yddemo:plantYear:report", name = "上传年指标", groupKey = "yddemo:plantYear", //
			isMenu = false, type = FunctionType.BUTTON)
	ResponseEntity<List<IndicatorData>> report(@RequestBody List<IndicatorDataPlant> dataPlants,
			@RequestParam(required = true) String date) {
		List<IndicatorData> datas = dataPlantService.reportIndicatorDataYears(dataPlants, date);
		return ResponseEntity.ok(datas);
	}

}
